package com.project.geonotes;

import java.util.ArrayList;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class CategoryDownloadService extends AsyncTask<Void, Void, Void> {
	private String username;
	private Context mContext;
	public CategoryDownloadService(String mUsername, Context context) {
		username = mUsername;
		mContext = context;
	}

	//	@Override
//	public IBinder onBind(Intent arg0) {
//		// TODO Auto-generated method stub
//		return null;
//	}
//	@Override
//	public int onStartCommand(Intent intent, int flags, int startId) {
//		super.onStartCommand(intent, flags, startId);
//		WebWrapper categories = null;
//		Bundle other = intent.getExtras();
//		String username = other.getString("username");
//		Log.d("username", username);
//		try {
//			categories = new WebWrapper(username);
//			Log.d("WEBWRAPPER", "Request successful");
//			
//		} catch (Exception e) {
//			Log.d("WEBWRAPPER", "Request NOT successful");
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		ArrayList<Category> toAdd = categories.getCategories();
//		Log.d("DOWNLOADED", toAdd.toString());
//		return startId;
//			
//	}
	@Override
	protected Void doInBackground(Void... arg0) {
		WebWrapper request = null;
		ArrayList<String> categories;
		//Bundle other = arg0[0].getExtras();
//		String username = other.getString("username");
		Log.d("username", username);
		try {
			request = new WebWrapper(username);
			Log.d("WEBWRAPPER", "Request successful");
			
		} catch (Exception e) {
			Log.d("WEBWRAPPER", "Request NOT successful");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ArrayList<Category> toAdd = request.getCategories();
		Log.d("DOWNLOADED", toAdd.toString());
		DBAdapter db = new DBAdapter(mContext);
		db.open();
		for (int i =0; i < toAdd.size(); i++) {
			Log.d("DOWNLOAD", "" + toAdd.get(i));
			db.addCategory(toAdd.get(i));
		}
		db.close();
		return null;
	}

}
